﻿<%@ Application Inherits="LIB.MyHttpApplication" Language="C#" %>
<%@ Import Namespace="System.IO.Compression" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    protected void Application_BeginRequest(object sender, EventArgs e)
    {

        HttpContext myContext = HttpContext.Current;
        string strRegex;
        Match match_rewrite;

        try
        {
            string curURL = myContext.Request.Path.ToString();
            string url = "~/" + curURL;
            string curURL1 = curURL.Replace(".html", "");
            String[] urlArray = curURL1.Split('/');
            if (curURL.Contains("html"))
            {
                if (curURL.Contains("/quan-huyen"))
                    url = "~/quan-huyen-detail.aspx?id=" + urlArray[2];
                else if (curURL.Contains("/muc-gia") || curURL.Contains("/loai-bat-dong-san") || curURL.Contains("/du-an-khu-do-thi"))
                    url = "~/danh-muc-nha.aspx?id=" + urlArray[2];
                else if (curURL.Contains("/bat-dong-san"))
                {
                    if (urlArray.Length > 4)
                        url = "~/danh-muc-nha-quan-huyen.aspx?quanHuyenID=" + urlArray[2] + "&mucGiaID=" + urlArray[3];
                    else
                        url = "~/chi-tiet-nha.aspx?id=" + urlArray[2];
                }
                else if (curURL.Contains("/tin-tuc"))
                {
                    if (urlArray.Length > 4)
                        url = "~/tin-tuc-detail.aspx?id=" + urlArray[4] + "&catID=" + urlArray[2];
                    else
                        url = "~/tin-tuc-list.aspx?id=" + urlArray[2];
                }
                else if (curURL.Contains("/thong-bao"))
                {
                    url = "~/thong-bao-detail.aspx?id=" + urlArray[3] + "&catID=" + urlArray[2];
                }
                else if (curURL.Contains("/tuyen-dung"))
                {
                    url = "~/tuyen-dung-detail.aspx?id=" + urlArray[3] + "&catID=" + urlArray[2];
                }
                else if (curURL.Contains("/thong-tin-du-an"))
                {
                    if (urlArray.Length > 4)
                        url = "~/thong-tin-du-an-detail.aspx?id=" + urlArray[4] + "&catID=" + urlArray[2];
                    else
                        url = "~/thong-tin-du-an-list.aspx?id=" + urlArray[2];
                }
                myContext.RewritePath(url,false);
                //Context.RewritePath(url, false);
            }
        }
        catch (Exception ex)
        {
            Response.Write("ERR in Global.asax :" + ex.Message + "\n\n" + ex.StackTrace.ToString() + "\n\n");
            myContext.RewritePath("~/errorpage.aspx");
            Response.Write("Lỗi không tìm thấy url hiện tại \n\n");
        }
        //try
        //{
        //    //###### begin use compress ###################
        //    HttpApplication app = (HttpApplication)sender;
        //    string acceptEncoding = app.Request.Headers["Accept-Encoding"];
        //    Stream prevUncompressedStream = app.Response.Filter;

        //    if (acceptEncoding == null || acceptEncoding.Length == 0)
        //        return;

        //    acceptEncoding = acceptEncoding.ToLower();
        //    string url1 = Request.Url.ToString();
        //    string _physicalPath = app.Request.Url.LocalPath;
        //    if (_physicalPath.Contains(".aspx")) //|| _physicalPath.Contains(".html"))
        //    {
        //        if (acceptEncoding.Contains("gzip"))
        //        {
        //            // gzip
        //            app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
        //            app.Response.AppendHeader("Content-Encoding", "gzip");
        //        }
        //        else if (acceptEncoding.Contains("deflate"))
        //        {
        //            // defalte
        //            app.Response.Filter = new DeflateStream(prevUncompressedStream, CompressionMode.Compress);
        //            app.Response.AppendHeader("Content-Encoding", "deflate");
        //        }
        //    }
        //}
        //catch { }
        ////########### end use compress ########################
    }  
</script>